import Vue from 'vue'
import App from './App'
import member from 'static/common/js/common.js'

Vue.config.productionTip = false

App.mpType = 'app'

// 引入全局uView
import uView from 'uview-ui'
Vue.use(uView)

// 此处为演示vuex使用，非uView的功能部分
import store from '@/store'

// 引入uView提供的对vuex的简写法文件
let vuexStore = require('@/store/$u.mixin.js')
Vue.mixin(vuexStore)

// 引入uView对小程序分享的mixin封装
let mpShare = require('uview-ui/libs/mixin/mpShare.js');
Vue.mixin(mpShare)

//  http://api.9colordeer.com/
var url_p = "http://api.9colordeer.com/";
Vue.prototype.update_url = "http://api.9colordeer.com/";
Vue.prototype.img_pre = "http://sybg.qiniu.isartop.com/";



Vue.prototype.identity = "";

const app = new Vue({
	store,
	...App
})


Vue.prototype.toHint = function(id) {
	this.$u.route("/pages/home/hint?id=" + id);
}
/**
 * 等级
 * @param {Object} data
 * @param {Object} list
 */
Vue.prototype.getGrade = function(data, list) {
	var json = {}
	json.score = 0;
	json.invite_count = 0;
	json.scoreX = 0;
	json.invite_countX = 0;
	json.privilege = data.privilege;
	if (parseInt(data.team_count) >= list.tuijian && parseInt(data.score) >= list.jingyan) {
		json.state = true;
	} else {
		json.invite_count = Vue.prototype.sub(list.tuijian, data.team_count)
		json.score = Vue.prototype.sub(list.jingyan, data.score)
		json.invite_countX = list.tuijian;
		json.scoreX = list.jingyan;
		json.state = false;
	}
	return json
}
/**
 * 点击查看大图
 * @param {String} e
 */
Vue.prototype.seeImage = function(e) {
	var arr = []
	var indicator = "none"
	if (typeof e == 'string') {
		arr.push(e)
	} else {
		arr = e
	}
	if (arr.length > 1) {
		indicator = "default"
	}
	uni.previewImage({
		urls: arr,
		indicator: indicator
	});
}

/**
 * 判断用户是否为默认头像
 * @param {String} e
 */
Vue.prototype.getHead = function(e) {
	if (!!e) {
		e = Vue.prototype.img_pre + e
	} else {
		e = "/static/image/head.png"
	}
	return e
}
/**
 * 画图圆形图片
 * @param {String} e
 */
Vue.prototype.circleImg = function(ctx, img, x, y, r) {
	ctx.save();
	var d = 2 * r;
	var cx = x + r;
	var cy = y + r;
	ctx.arc(cx, cy, r, 0, 2 * Math.PI);
	ctx.clip();
	ctx.drawImage(img, x, y, d, d);
	ctx.restore();
	console.log(ctx);
}
/**
 * 获取验证码按钮倒计时
 * @param {Object} than
 */
Vue.prototype.getCode = function(than) {
	than.isDisable = true;
	var codeText = than.codeText
	var clock = setInterval(function() {
		than.totalTime--
		than.codeText = than.totalTime + 's'
		if (than.totalTime == 1) {
			clearInterval(clock);
			than.codeText = codeText
			than.isDisable = false;
			than.totalTime = 60
		}
	}, 1000)
}
//返回上一页传值
Vue.prototype.setBackData = function(name, data) {
	try {
		uni.setStorageSync(name, data);
	} catch (e) {
		// error
	}
	uni.navigateBack();
}
//返回上一页取值
Vue.prototype.getBackData = function(name) {
	var json = {};
	try {
		json = uni.getStorageSync(name);
		if (json) {
			uni.removeStorageSync(name);
		}
	} catch (e) {
		// error
	}
	return json
}
//跳转页面
Vue.prototype.toPage = function(url, toast) {
	if (toast) {
		Vue.prototype.toast(toast)
	} else {
		uni.navigateTo({
			url: url
		})
	}
}
Vue.prototype.getMoney = function(money) {
	var m = Math.floor(money);
	if (m > 10000) {
		m = Math.floor(m / 10000) + "万"
	}
	return m
}
/**
 * 上传post
 * @param {Object} url
 * @param {Object} filePath
 * @param {Object} name
 * @param {Object} cb
 * @param {Object} error_cb
 */
Vue.prototype.uploadPost = function(url, filePath, name, cb, error_cb) {
	var token = null;
	var data = {};
	token = uni.getStorageSync('token');
	if (token != null) {
		data.token = token
	}
	uni.uploadFile({
		url: url_p + url,
		filePath: filePath,
		name: name,
		formData: data,
		success: (res) => {
			var json = JSON.parse(res.data);
			if (json.error_code == 0) {
				cb(json.result)
			} else {
				if (json.error_code == 1) {
					if (error_cb == null) {
						Vue.prototype.toast(json.message)
					} else {
						Vue.prototype.toast(json.message)
					}
				} else if (json.error_code == 301) {
					Vue.prototype.toLogin()
				} else {
					Vue.prototype.toast(json.message, json.error_code)
				}
			}
		},
		fail(e) {
			var message = "网络异常"
			Vue.prototype.toast(message)
		}
	});
}
Vue.prototype.post = function(url, data, cb, error_cb) {
	if (data == null) {
		data = {}
	}
	var token = null;
	token = uni.getStorageSync('token');
	if (token != null) {
		data.token = token
	}
	uni.request({
		url: url_p + url,
		data: data,
		method: "POST",
		dataType: "json",
		success: (res) => {
			if (res.data.error_code == 0) {
				cb(res.data.result)
			} else {
				if (res.data.error_code == 1) {
					Vue.prototype.toast(res.data.message)
				} else if (res.data.error_code == 301) {
					Vue.prototype.toLogin()
				} else if (res.data.error_code == 401) {
					Vue.prototype.toFreeze()
				} else if (res.data.error_code == 501) {
					Vue.prototype.toMaintain()
				} else {
					Vue.prototype.toast('网络繁忙')
				}
			}
		},
		fail: (mas) => {
			console.log(mas)
			var message = "网络异常"
			Vue.prototype.toast(message)
			var errcode = msg.status;
			if (errcode == 500 || errcode == 404) {
				message = "服务器异常"
			}
			if (error_cb != null) {
				error_cb(message, -1)
			}
			console.log(mas)
		}

	})

};
/**
 *判断对象有没有值
 */
Vue.prototype.checkDataNull = function(checkData) {
	if (checkData != '' && checkData != undefined && checkData != null && checkData != NaN) {
		return true;
	}
	return false;
};

/**
 * 获取当前时间
 */
Vue.prototype.getNowFormatDate = function(newdate) {
	var date = null;
	if (!!newdate) {
		date = new Date(newdate * 1000);
	} else {
		date = new Date();
	}
	var seperator1 = ".";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
};
Vue.prototype.endYear = Number(Vue.prototype.getNowFormatDate().substring(0, Vue.prototype.getNowFormatDate().length -
	6))

/**
 * 验证手机号
 */
Vue.prototype.checkPhone = function(phone) {
	if (!(/^1[3456789]\d{9}$/.test(phone))) {
		return false;
	} else {
		return true;
	}
};
/**
 * 验证邮箱
 */
Vue.prototype.checkEmail = function(email) {
	if (!(/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(email))) {
		return false;
	} else {
		return true;
	}
};
/**
 * 提示框
 */
Vue.prototype.toast = function(text) {
	uni.showToast({
		title: text,
		icon: 'none',
		duration: 2000
	})
}
/**
 * 验证密码强度
 */
Vue.prototype.checkPassWord = function(password) {
	var str = password;
	if (str == null || str.length < 6) {
		return false;
	}
	var reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
	if (reg.test(str))
		return true;
}
/**
 * 加法
 */
Vue.prototype.add = function(a, b) {
	var c, d, e;
	try {
		c = a.toString().split(".")[1].length;
	} catch (f) {
		c = 0;
	}
	try {
		d = b.toString().split(".")[1].length;
	} catch (f) {
		d = 0;
	}
	return e = Math.pow(10, Math.max(c, d)), (Vue.prototype.mul(a, e) + Vue.prototype.mul(b, e)) / e;
}
/**
 * 减法
 */
Vue.prototype.sub = function(a, b) {
	var c, d, e;
	try {
		c = a.toString().split(".")[1].length;
	} catch (f) {
		c = 0;
	}
	try {
		d = b.toString().split(".")[1].length;
	} catch (f) {
		d = 0;
	}
	return e = Math.pow(10, Math.max(c, d)), (Vue.prototype.mul(a, e) - Vue.prototype.mul(b, e)) / e;
}
/**
 * 乘法
 */
Vue.prototype.mul = function(a, b) {
	var c = 0,
		d = a.toString(),
		e = b.toString();
	try {
		c += d.split(".")[1].length;
	} catch (f) {}
	try {
		c += e.split(".")[1].length;
	} catch (f) {}
	return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}
/**
 * 除法
 */
Vue.prototype.div = function(a, b) {
	var c, d, e = 0,
		f = 0;
	try {
		e = a.toString().split(".")[1].length;
	} catch (g) {}
	try {
		f = b.toString().split(".")[1].length;
	} catch (g) {}
	return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), Vue.prototype.mul(
		c / d,
		Math.pow(10,
			f - e));

}
/**
 * 分享图片到微信好友
 * @param {Object} imageUrl
 */
Vue.prototype.friend = function(imageUrl) {
	// #ifdef APP-PLUS
	uni.share({
		provider: "weixin",
		scene: "WXSceneSession",
		type: 2,
		imageUrl: imageUrl,
		success: function(res) {
			uni.hideLoading();
		},
		fail: function(err) {
			uni.hideLoading();
		}
	});
	// #endif
	uni.hideLoading();
}
/**
 * 分享图片到朋友圈
 * @param {Object} imageUrl
 */
Vue.prototype.circle = function(imageUrl) {
	// #ifdef APP-PLUS
	uni.share({
		provider: "weixin",
		scene: "WXSenceTimeline",
		type: 2,
		imageUrl: imageUrl,
		success: function(res) {
			uni.hideLoading();
		},
		fail: function(err) {
			uni.hideLoading();
		}
	});
	// #endif
	uni.hideLoading();
}
/**
 * 下载图片到手机相册
 * @param {Object} imageUrl
 */
Vue.prototype.download = function(imageUrl) {
	// #ifdef H5
	Vue.prototype.seeImage(imageUrl)
	// #endif
	// #ifndef H5
	uni.saveImageToPhotosAlbum({
		filePath: imageUrl,
		success: function(res) {
			Vue.prototype.toast(res)
			Vue.prototype.toast("下载成功")
			uni.hideLoading();
		},
		fail: function(res) {
			Vue.prototype.toast(res)
			Vue.prototype.toast("下载失败 ")
			uni.hideLoading();
		}
	});
	// #endif
}

/**
 * 跳转登录
 */
Vue.prototype.toLogin = function(imageUrl) {
	/* uni.reLaunch({
		url: '/pages/htmlA/login/index'
	}); */
	uni.reLaunch({
		url: '/pages/htmlA/login1/index'
	});
	return false
}
/**
 * 跳转冻结
 */
Vue.prototype.toFreeze = function() {
	uni.reLaunch({
		url: '/pages/index/freeze'
	});
	return false
}
/**
 * 跳转维护
 */
Vue.prototype.toMaintain = function() {
	uni.reLaunch({
		url: '/pages/index/maintain'
	});
	return false
}
app.$mount()
